package com.hm.ruiji.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hm.ruiji.common.CustomException;
import com.hm.ruiji.dto.SetmealDto;
import com.hm.ruiji.entity.Setmeal;
import com.hm.ruiji.entity.SetmealDish;
import com.hm.ruiji.mapper.SetMealMapper;
import com.hm.ruiji.service.SetMealDishService;
import com.hm.ruiji.service.SetMealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class SetMealServiceImpl extends ServiceImpl<SetMealMapper, Setmeal> implements SetMealService {

    @Resource
    private SetMealDishService setMealDishService;

    /**
     * 保存套餐信息
     * @param setmealDto
     */
    @Override
    @Transactional
    public void saveWithDish(SetmealDto setmealDto) {
        // 保存套餐基本信息
        this.save(setmealDto);

        // 获取套餐关联菜品
        List<SetmealDish> dishList = setmealDto.getSetmealDishes();
        dishList.stream().map((item)-> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());

        // 保存套餐和菜品关联信息
        setMealDishService.saveBatch(dishList);


    }

    /**
     * 根据id查询套餐及关联菜品信息
     * @param id
     * @return
     */
    @Override
    public SetmealDto getByIdWithDish(Long id) {
        // 创建对象存储信息
        Setmeal setmeal = this.getById(id);
        SetmealDto setmealDto = new SetmealDto();

        // 根据套餐id查询关联菜品信息
        LambdaQueryWrapper<SetmealDish> setmealDishWrapper = new LambdaQueryWrapper<>();
        setmealDishWrapper.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> list = setMealDishService.list(setmealDishWrapper);

        // 将菜品信息复制到dto类
        /**
         * 拷贝对象
         * Params:
         * source – the source bean
         * target – the target bean
         */
        BeanUtils.copyProperties(setmeal, setmealDto);
        setmealDto.setSetmealDishes(list);
        return setmealDto;
    }

    /**
     * 修改套餐信息
     * @param setmealDto
     */
    @Override
    public void updateWithDish(SetmealDto setmealDto) {
        // 更新套餐基本信息
        this.updateById(setmealDto);

        // 删除关联菜品信息
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, setmealDto.getId());
        setMealDishService.remove(wrapper);

        // 重新插入关联菜品信息
        setMealDishService.saveBatch(setmealDto.getSetmealDishes());
    }

    /**
     * 删除套餐和套餐菜品关系
     * @param ids
     */
    @Override
    public boolean removeWithDish(List<Long> ids) {
        //删除套餐表基本信息
        LambdaQueryWrapper<Setmeal> setmealWrapper = new LambdaQueryWrapper<>();
        setmealWrapper.in(Setmeal::getId, ids);
        setmealWrapper.eq(Setmeal::getStatus, 1); // 删除停售的  0停售 1在售

        int count = this.count(setmealWrapper);
        if (count > 0) {
            log.error("你选中的套餐中包含在售产品，请停售后再删除");
            return false;
        }

        this.removeByIds(ids);

        //删除套餐菜品关系表
        LambdaQueryWrapper<SetmealDish> setmealDishWrapper = new LambdaQueryWrapper<>();
        setmealDishWrapper.eq(SetmealDish::getSetmealId, ids);
        setMealDishService.remove(setmealDishWrapper);
        return true;
    }
}
